﻿using System;
using System.Collections.Generic;
using System.Text;
using Chaliy.DbPro.Generators.TextAuthoring.Configuration;

namespace Chaliy.DbPro.Generators.TextAuthoring
{
	public class RandomParagraphAuthor : IParagraphAuthor
	{
		#region Fields

		private Random randomSelector;		
		private readonly ISentanceAuthor sentanceAuthor;
		private int minLength;
		private int maxLength;
		
		#endregion

		#region Constructors

		public RandomParagraphAuthor(ISentanceAuthor sentanceAuthor)
		{			
			if (sentanceAuthor == null)
			{
				throw new ArgumentNullException("sentanceAuthor");
			}			
			this.sentanceAuthor = sentanceAuthor;
		}

		#endregion

		#region Properties

		protected Random RandomSelector
		{
			get
			{
				if (this.randomSelector == null)
				{
					this.randomSelector = new Random();
				}
				return this.randomSelector;
			}
		}
		
		protected ISentanceAuthor SentanceAuthor
		{
			get
			{
				return this.sentanceAuthor;
			}
		}

		public int MinLength
		{
			get
			{
				return this.minLength;
			}
			set
			{
				this.minLength = value;
			}
		}

		public int MaxLength
		{
			get
			{
				return this.maxLength;
			}
			set
			{
				this.maxLength = value;
			}
		}
		

		#endregion

		#region IParagraphAuthor Members

		public string Generate()
		{
			int minLenght = this.MinLength;
			int maxLenght = this.MaxLength;

			int lengh = this.RandomSelector.Next(minLenght, maxLenght);

			StringBuilder paragraphBuffer = new StringBuilder();

			for (int i = 0; i < lengh; i++)
			{
				if (i > 0)
				{
					paragraphBuffer.Append(" ");
				}
				paragraphBuffer.Append(this.GenerateSentance());								
			}

			return paragraphBuffer.ToString();
		}		

		#endregion

		#region Methods

		protected virtual string GenerateSentance()
		{
			return this.SentanceAuthor.Generate();
		}

		#endregion
	}
}
